const app = getApp();
import { getWePhoneNumber } from "../../../api/homePage"
import { feedbackAdd, getUserInfo } from "../../../api/my"
import { getStorage } from "../../../utils/localStorage"
Page({
    data: {
        navData: {
            level: 2,
            title: '意见反馈'
        },
        form: {
            content: null,
            telephone: null,
        },
        user: getStorage('user'),
        navMargin: app.globalData.navHeight + app.globalData.navTop
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            "form.telephone": this.data.user.phoneNumber
        });
    },
    //反馈内容
    content(e) {
        const value = e.detail.value;
        this.setData({
            "form.content": value,
        });
    },
    //获取用户手机号
    async getPhoneNumber(e) {
        if (e.detail.errMsg === "getPhoneNumber:ok") {
            let res = await getWePhoneNumber({ code: e.detail.code });
            let { code, data } = res.data;
            if (code === 200) {
                this.setData({
                    "form.telephone": data.phoneNumber
                });
                this.getWxUser();
            };
        };
    },
    //获取用户信息
    async getWxUser() {
        let _this = this.data;
        let userRes = await getUserInfo({ id: _this.user.id });
        const { code, data } = userRes.data;
        if (code === 200) {
            data.username = data.userName;
            setStorage('user', data);
        };
    },
    //提交
    async submit() {
        let _this = this.data;
        if (!_this.form.content && !_this.form.telephone) {
            wx.showToast({
                icon: 'none',
                title: '意见反馈内容和联系方式不能为空!!!',
            });
        } else {
            let res = await feedbackAdd(_this.form);
            if (res.data.code === 200) {
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 1000
                });
                setTimeout(() => { wx.navigateBack(); }, 1100)
            };
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})